#include "timer0.h"
#include <mcs51/8052.h>


void Timer0_init()
{

    TMOD &= 0xF0; // 设置定时器模式
    TMOD |= 0x01; // 设置定时器模式
    TL0 = 0;      // 设置定时初值
    TH0 = 0;      // 设置定时初值
    TF0 = 0;      // 清除TF0标志
    TR0 = 0;      // 定时器0不计时
}

unsigned int Timer0_start()
{
    TR0 = 1;
}

/**
 *  返回值范围：0~65535
 *
 *  12Hz 每个机器周期 1us
 *  11.0952Hz 每个机器周期 1.085us
 */
unsigned int Timer0_getTime()
{
    unsigned int value = (TH0 << 8) | TL0;

    // 11.0952Hz 需要处一下
    value *= 1.085;
    return value;
}

unsigned int Timer0_close()
{
    unsigned int value = Timer0_getTime();
    TR0 = 0;
    TF0 = 0;
    return value;
}

unsigned int Timer0_restart()
{
    unsigned int value = Timer0_getTime();
    TR0 = 0; // 定时器0开关
    TH0 = 0x00; // 设置定时初值
    TL0 = 0x00; // 设置定时初值
    TF0 = 0;
    TR0 = 1;
    return value;
}